<%-- 
    Document   : checkout.jsp
    Created on : 11-May-2011, 19:08:35
    Author     : Arsonist
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<jsp:useBean id="user" class="checkout.CheckoutInfo" scope="session"/>
<jsp:setProperty name="user" property="*"/>
<jsp:useBean id="login" class="login.LoginInfo" scope="session"/>

<%@ include file="header.jsp" %>

<!-- Content -->
<div id="container">
	<div id="content" role="main">
		<div id="post-0" class="post error404 not-found">
			<h1 class="entry-title">Checkout</h1>
			<div class="entry-content">
                            <p>Use the checkout to buy books from eBookCafe.</p>
                            <% //If user logged in asks for payment details
                                if(session.getAttribute("loggedin_username") != null){
                                    if(session.getAttribute("cart") != null){
                                        user.setAllNull();

                                        out.println("<h2>Books you have selected to purchase:</h2><p></p>");

                                        float price = 0;
                                        String cartContentsString = (String) session.getAttribute("cart");
                                        String[] cartContents = cartContentsString.split(",");

                                        bookDatabase_pkg.bookDatabaseDriver driver1 = new bookDatabase_pkg.bookDatabaseDriver();
                                        String category1 = "action";
                                        int result1 = driver1.databaseConnect(category1);

                                        out.println("<table>");
                                        out.println("<tr><td><b>Name</b></td><td colspan=2><b>Price (£)</b></td></tr>");

                                        if(result1 == 0){
                                            for(int i=0; i<cartContents.length; i++){
                                                String[] book1 = driver1.getBook(cartContents[i]);
                                                if(book1[0] != null){
                                                    out.println("<tr><td>" + book1[1] + "</td><td colspan=2>" + book1[3] + "</td></tr>");
                                                    price+= Float.parseFloat(book1[3]);
                                                }
                                            }
                                        }
                                
                                        driver1 = new bookDatabase_pkg.bookDatabaseDriver();
                                        category1 = "drama";
                                        result1 = driver1.databaseConnect(category1);

                                        if(result1 == 0){
                                            for(int i=0; i<cartContents.length; i++){
                                                String[] book1 = driver1.getBook(cartContents[i]);
                                                if(book1[0] != null){
                                                    out.println("<tr><td>" + book1[1] + "</td><td colspan=2>" + book1[3] + "</td></tr>");
                                                    price+= Float.parseFloat(book1[3]);
                                                }
                                            }
                                        }
                                
                                        driver1 = new bookDatabase_pkg.bookDatabaseDriver();
                                        category1 = "comedy";
                                        result1 = driver1.databaseConnect(category1);

                                        if(result1 == 0){
                                            for(int i=0; i<cartContents.length; i++){
                                                String[] book1 = driver1.getBook(cartContents[i]);
                                                if(book1[0] != null){
                                                    out.println("<tr><td>" + book1[1] + "</td><td colspan=2>" + book1[3] + "</td></tr>");
                                                    price+= Float.parseFloat(book1[3]);
                                                }
                                            }
                                        }
                                
                                        out.println("<tr><td><b>Total:</b></td><td colspan=2>"+ String.format("%.2f", price) + "</td></tr>");
                                        out.println("</table>");
                            
                                        out.println("<h2>Please enter your Payment details:</h2>");

                                        if(request.getQueryString() != null && request.getQueryString().contains("details")){
                                            out.println("<p><font color='red'>Details were incorrect, please re-enter!</font></p>");
                                        } 
                                        
                                        out.println("<p></p>");
                                        %>
                                        <form action ="docheckout.jsp" method=post>
                                            <table>
                                                <tr><td>Card type:</td>
                                                    <td>
                                                        <select name="cardType">
                                                            <option>Select</option>
                                                            <option value="VISA">Visa</option>
                                                            <option value="MASTERCARD">MasterCard</option>
                                                            <option value="SOLO">Solo</option>
                                                            <option value="SWITCH">Switch</option>
                                                            <option value="VISADEBIT">Visa Debit</option>
                                                        </select>
                                                    </td>
                                                <tr><td>Card number:</td><td colspan=2><input type="text" name="cardNumber" value="" size=25  maxlength=16></td></tr>
                                                <tr><td>Card holder's Name:</td><td colspan=2><input type="text" name="cardHolder" value="" size=25  maxlength=50></td></tr>
                                                <tr><td>Address:</td><td colspan=2><textarea name="cardAddress" rows="1" cols="23"></textarea></td></tr>
                                                <tr><td>Postcode:</td><td colspan=2><input type="text" name="cardPostcode" value="" size=25  maxlength=9></td></tr>
                                                <tr><td>Expiry Date (MM/YY):</td><td colspan=2> <input type="text" name="expiryMonth" size=2 value="" maxlength=2><input type="text" name="expiryYear" size=2 value="" maxlength=2></td></tr>
                                                <tr><td>Security code:</td><td colspan=2><input type="text" name="securityCode" size=3 value="" maxlength=3></td></tr>

                                                <input type="hidden" value="<%=price%>" name="price" />

                                                <tr><td><input type="submit" value="Submit" ></td><td><input type="reset"  value="Reset"></td></tr>
                                            </table>
                                        </form>

                                <% }
                                    else{
                                        out.println("There are no items in your Shopping cart.");
                                    }
                                }

                                //If user is not logged in asks to log in before proceeding
                                else{
                                    session.setAttribute("currentPage", request.getRequestURL()+"?checkout");
                                    out.println("Please sign in to proceed:"); %>
                                    <p></p>
                                    <form action="dologin.jsp" method=post>
                                    <table>
                                        <tr><td>Username: </td><td colspan=2><input type="text" name="username" value="" size=20  maxlength=50></td></tr>
                                        <tr><td>Password: </td><td colspan=2><input type="password" name="password" value="" size=20  maxlength=20></td></tr>
                                        <tr><td colspan=2></td><td><a HREF="checkout.jsp"><input type="submit" value="Submit"></a> <input type="reset"  value="Reset"></td></tr>
                                    </table>
                                    </form>

                                <% } %>
			</div><!-- .entry-content -->
		</div><!-- #post-0 -->

	</div><!-- #content -->
</div><!-- #container -->

<%@ include file="sidebar.jsp" %>

<!-- /Content -->

<%@ include file="footer.jsp" %>
